﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// search_result 的摘要说明
    /// </summary>
    public class search_result : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        public void ProcessRequest(HttpContext context)
        {
            string keyword = Utils.ToHtml(DTRequest.GetQueryString("keyword"));

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取图库信息
            BLL.photos qz_bll = new BLL.photos();
            DataSet qz_ds = qz_bll.GetList(10, "title like '%" + keyword + "%'", "id desc");
            //如果记录存在
            if (qz_ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"photo_list\":[");
                for (int i = 0; i < qz_ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = qz_ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    string img_url = dr["img_url"].ToString();
                    if (img_url.Contains("shicaiquanzi"))
                    {
                        if (img_url.Contains("http://"))
                        {
                            strTxt.Append(",\"img_url\":\"" + img_url + "\"");

                        }
                        else { strTxt.Append(",\"img_url\":\"http://" + img_url + "\""); }
                        //strTxt.Append(",\"img_url\":\"http://" + img_url + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + dr["img_url"] + "\"");
                    }
                    string province_name = new BLL.province().GetTitle(Convert.ToInt32(dr["province_id"]));
                    string city_name = new BLL.city().GetTitle(Convert.ToInt32(dr["city_id"]));
                    string district_name = new BLL.district().GetTitle(Convert.ToInt32(dr["district_id"]));
                    strTxt.Append(",\"diqu\":\"" + province_name + city_name + district_name + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < qz_ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            else
            {
                strTxt.Append("\"photo_list\":[],");
            }

            //获取企业信息
            BLL.company qg_bll = new BLL.company();
            DataSet qg_ds = qg_bll.GetList(10, 1, "title like '%" + keyword + "%' or pro_remark like '%" + keyword + "%' and status=1", "sort_id desc,group_id desc,id asc", out this.totalCount);
            //如果记录存在
             int xx_total_page = this.totalCount / 10;
            strTxt.Append("\"total_page\":" + xx_total_page.ToString()+",");
            if (qg_ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"company_list\":[");
                for (int i = 0; i < qg_ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = qg_ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    string _logo = dr["logo"] + "";
                    if (string.IsNullOrEmpty(_logo))
                    {
                        strTxt.Append(",\"img_url\":\"../image/default_avatar.png\"");
                    }
                    else if (_logo.Contains("shicaiquanzi"))
                    //if (_logo.Contains("shicaiquanzi"))
                    {
                        if (_logo.Contains("http://")) { strTxt.Append(",\"img_url\":\"" + _logo + "\""); }
                        else { strTxt.Append(",\"img_url\":\"http://" + _logo + "\""); }
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + _logo + "\"");
                    }
                    //strTxt.Append(",\"img_url\":\"http://" + dr["logo"] + "\"");
                    strTxt.Append(",\"pro_remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr["pro_remark"].ToString()) + "\"");
                    strTxt.Append(",\"group_id\":" + dr["group_id"]);
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < qg_ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            else
            {
                strTxt.Append("\"company_list\":[],");
            }
            //获取供应信息
            BLL.gongying bll5 = new BLL.gongying();
            DataSet ds5 = bll5.GetList(10, "title like '%" + keyword + "%' or content like '%" + keyword + "%' and status=1", "id desc");
            //如果记录存在
            if (ds5.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"gongying_list\":[");
                for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
                {
                    DataRow dr5 = ds5.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr5["id"]);
                    strTxt.Append(",\"title\":\"" + dr5["title"] + "\"");
                    strTxt.Append(",\"size\":\"" + dr5["size_long"] + "×" + dr5["size_wide"] + "×" + dr5["size_height"] + "\"");

                    string gongying_img = dr5["img_url"].ToString();
                    if (string.IsNullOrEmpty(gongying_img))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else if (gongying_img.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"img_url\":\"http://" + gongying_img + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http:\\/\\/pic.shicaiquanzi.com" + gongying_img + "\"");
                    }
                    strTxt.Append(",\"lianxiren\":\"" + dr5["lianxiren"] + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr5["add_time"])) + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds5.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            else
            {
                strTxt.Append("\"gongying_list\":[],");
            }
            //推荐企业
            BLL.qiugou bll2 = new BLL.qiugou();
            DataSet ds2 = bll2.GetList(10, "title like '%" + keyword + "%' or content like '%" + keyword + "%'", "id desc");
            //如果记录存在
            if (ds2.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"qiugou_list\":[");
                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds2.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append(",\"size\":\"" + dr["size_long"] + "×" + dr["size_wide"] + "×" + dr["size_height"] + "\"");
                    string _img_url = dr["img_url"].ToString();
                    if (string.IsNullOrEmpty(_img_url))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else if (_img_url.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"img_url\":\"http://" + _img_url + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + _img_url + "\"");
                    }
                    strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds2.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"qiugou_list\":[]");
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}